Cat US

Use these results without warranty or express or implied fitness for any purpose. The results are not vetted for accuracy. The results are not vetted for completeness. The results are not vetted for usability

State counts

Total Cats per State

Count of Tracks by County

Cat wins

cat counts per state

States Won per Cat

cat counts per county

Counties Won per Cat

Row

cats represented in the analysis

x
Rye8
Big Papa
Rye13
Bigger Papa
sofia-moto-fdb7
tonga-moto-63b2
RyePhone
sofia-moto-0538
sofia-moto-099b
sofia-moto-3582
sofia-moto-fc37
Lil Papa
iha
sofia-moto-084b
sofia-moto-3965
sofia-moto-555d
sofia-moto-6c2f
sofia-moto-70c2
sofia-moto-7c9a
sofia-moto-80cb
sofia-moto-9062
sofia-moto-95c9
sofia-moto-a24e
sofia-moto-b904
sofia-moto-bf20
sofia-moto-e67d
Papa G
sofia-moto-8302
sofia-moto-bf28
sofia-moto-d36e
LilPapa

cats not represented in the analysis

x
Chishiki
Twenty7
Kitty’s MacBook Pro
iPhone 8
Big Mamma
KK’s Work Phone
iPhone
Jonathan Carlomagno’s iPhone
Bob’s iPhone 6
Rathbone
Kayleigh’s iPhone
jl
marlin-Pixel-222d
jlc
P2
kek
pancho
moto-i-2
JLC
ubp52
Pam Ardis iPhone
tester
QP1A.191005.007.A3/Pixel XL
QPMS30.80-51-5/moto g power
QPMS30.80-51-8/moto g power
---
title: "geoCat"
output: 
  flexdashboard::flex_dashboard:
    vertical_layout: scroll
    orientation: rows
    social: menu
    source_code: embed
---

# Cat US

Use these results without warranty or express or implied fitness for any purpose.  The results are not vetted for accuracy.  The results are not vetted for completeness.  The results are not vetted for usability

- Limited to results from IACat and RyeCat

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)


library(data.table)
library(highcharter)
library(dplyr)
library(knitr)
library(maps)
library(plotly)
data("county.fips")
data("uscountygeojson")
data("usgeojson")

theme_set(theme_minimal())
#the COUNTYFP column is a character column, so we don't want to convert it to numeric
# df <- fread("../output/output_county_count.csv_combined.csv", colClasses = c("COUNTYFP" = "character"))
df <-
  fread(
    "../output/output_state_count.csv_combined.csv",
    colClasses = c("COUNTYFP" = "character")
  )
# create a vector of the names that need to be mapped
namesToMap <- c("Rye8", "RyePhone", "Rye13")
# replace the names in the Name column with "RyeCat"
# if the names are not in the Name column, throw a warning
if(any(!namesToMap %in% df$Name)) warning("Some names are not in the data frame")

df$OriginalName <- df$Name
df$Name[df$Name %in% namesToMap] <- "RyeCat"



# get the names that start with sofia or that contain Papa or that start with tonga
tongaNames <- df$Name[grepl("^tonga", df$Name, ignore.case = TRUE)]
papaNames <- df$Name[grepl("papa", df$Name, ignore.case = TRUE)]
sofiaNames <- df$Name[grepl("^sofia", df$Name, ignore.case = TRUE)]
IACatNames <- c(tongaNames, papaNames, sofiaNames, "iha")
iaCatPatterns= c("^tonga", "papa", "^sofia", "iha")

# map the IACatNames to IACat
df$Name[df$Name %in% IACatNames] <- "IACat"

# print the unique names that are not RyeCat or IACat
# kable(table()
missingNames <- unique(df$Name[!df$Name %in% c("RyeCat", "IACat")])


# subset the data frame to only include the rows where the Name column is equal to "RyeCat" or "IACat"
df <- df[Name %in% c("RyeCat", "IACat"), ]

```


```{r echo=FALSE, message=FALSE, warning=FALSE}

# function to sum the counts column by a given column name
sum_by_column <- function(in_df, column_names) {
  #group by the list of column names and sum the counts column
  in_df <- in_df[, .(counts = sum(counts)), by = column_names]
  return(in_df)
}

```

## State counts
   
### Total Cats per State
```{r}
sumByState <-
  sum_by_column(in_df = df, column_names = c("STATE_NAME"))
# log10 transform the counts column
sumByState$counts <- log10(sumByState$counts)
#create a chlorealpleth map of the US
highchart() %>%
  hc_add_series_map(
    usgeojson,
    sumByState,
    value = "counts",
    joinBy = c("name", "STATE_NAME"),
    name = "Count",
    dataLabels = list(enabled = TRUE, format = '{point.name}')
  ) %>%
  hc_mapNavigation(enabled = TRUE) %>%
  hc_colorAxis(stops = color_stops()) %>%
  hc_title(text = "Count of Tracks by State") %>%
  hc_add_theme(hc_theme_smpl()) %>%
  hc_legend(enabled = TRUE) %>%
  hc_tooltip(pointFormat = "{point.name}: {point.value}")

```


### Count of Tracks by County



```{r}
# TODO where is California counties
df$COUNTY_FIP=paste0(df$STATEFP,df$COUNTYFP)
sumByCounty <-
  sum_by_column(in_df = df, column_names = c("COUNTY_FIP"))
# log10 transform the counts column
sumByCounty$counts <- log10(sumByCounty$counts)
#create a chlorealpleth map of the US
highchart() %>%
  hc_add_series_map(
    uscountygeojson,
    sumByCounty,
    value = "counts",
    joinBy = c("fips", "COUNTY_FIP"),
    name = "Count") %>%
  hc_mapNavigation(enabled = TRUE) %>%
  hc_colorAxis(stops = color_stops()) %>%
  hc_title(text = "Count of Tracks by County") %>%
  hc_add_theme(hc_theme_smpl()) %>%
  hc_legend(enabled = FALSE) 
# %>%
#   hc_tooltip(pointFormat = "{point.name}: {point.value}")

```



## Cat wins

### cat counts per state

```{r}
# sum the counts column by the Name and STATE_NAME columns
sumByCatStateFull <-
  sum_by_column(in_df = df, column_names = c("Name", "STATE_NAME"))

sumByCatStateWin <-
  sumByCatStateFull[, .SD[which.max(counts)], by = STATE_NAME]

# create a color palette that maps RyeCat to bl
colorPalette <- c("#0000FF","#FF0000")
# create a named vector of colors
colorVector <- setNames(colorPalette, unique(sumByCatStateWin$Name))
# replace the color column with the color vector
sumByCatStateWin$color <- colorVector[sumByCatStateWin$Name]
# log10 transform the counts column
#create a chlorealpleth map of the US
highchart() %>%
  hc_add_series_map(
    usgeojson,
    sumByCatStateWin,
    value = "counts",
    joinBy = c("name", "STATE_NAME"),
    name = "Cat Winner",
    dataLabels = list(enabled = TRUE, format = '{point.name}'),color =colorVector
  ) %>%
  hc_mapNavigation(enabled = TRUE) %>%
  hc_title(text = "Which Cat Wins Which State (+DC") %>%
  hc_add_theme(hc_theme_smpl()) %>%
  hc_legend(enabled = FALSE) %>%
  hc_tooltip(pointFormat = "{point.name} is won by {point.Name}: {point.value} total tracks")


```

### States Won per Cat {data-width=250}



```{r}


# count the number of states that each cat won
total = sumByCatStateWin[, .N, by = Name]
#create a ggplot bar chart of the number of states that each cat won
# label by the cat name
# color by the colorPalette
# order by the number of states won
g = ggplot(total, aes(x = Name, y = N, fill = Name)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = Name), vjust = -0.5, size = 3) +
  scale_fill_manual(values = colorVector) +
  labs(x = "Cat", y = "Number of States(+DC) Won") +
  theme_minimal() +
  theme(legend.position = "none")

ggplotly(g)


```


### cat counts per county

```{r}
# sum the counts column by the Name and STATE_NAME columns
sumByCatCountyFull <-
  sum_by_column(in_df = df,
                column_names = c("Name", "COUNTY_FIP"))
sumByCatCountyWin <-
  sumByCatCountyFull[, .SD[which.max(counts)], by = COUNTY_FIP]
sumByCatCountyWin$color <- colorVector[sumByCatCountyWin$Name]

#create a chlorealpleth map of the US
highchart() %>%
  hc_add_series_map(
    uscountygeojson,
    sumByCatCountyWin,
    value = "counts",
    joinBy = c("fips", "COUNTY_FIP"),
    name = "Cat Winner",
    color = colorVector
  ) %>%
  hc_mapNavigation(enabled = TRUE) %>%
  hc_title(text = "Which Cat Wins Which County") %>%
  hc_add_theme(hc_theme_smpl()) %>%
  hc_legend(enabled = FALSE) %>%
  hc_tooltip(pointFormat = "{point.name} is won by {point.Name}: {point.value} total tracks")

```

### Counties Won per Cat {data-width=250}

```{r}
# count the number of states that each cat won
total = sumByCatCountyWin[, .N, by = Name]
#create a ggplot bar chart of the number of states that each cat won
# label by the cat name
# color by the colorPalette
# order by the number of states won
g=ggplot(total, aes(x = Name, y = N, fill = Name)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = Name), vjust = -0.5, size = 3) +
  scale_fill_manual(values = colorVector) +
  labs(x = "Cat", y = "Number of Counties Won") +
  theme_minimal() +
  theme(legend.position = "none")
ggplotly(g)


```

Row {.tabset .tabset-fade}
-------------------------------------


### cats represented in the analysis

```{r}

kable(unique(df$OriginalName))

```

### cats not represented in the analysis


```{r}
kable(missingNames)

```